package gbench.appdemo.crawler.junit;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.FSDirectory;
import org.junit.jupiter.api.Test;

import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import gbench.appdemo.crawler.luke.documents.DocumentsFactory;
import gbench.appdemo.crawler.luke.overview.OverviewFactory;

import static gbench.common.tree.LittleTree.IRecord.*;

/**
 * 
 * @author gbench
 *
 */
public class JunitLuke {
    
   @Test
   public void overview() {
       System.out.println("\n-------overview-------");
       
       final var ovfac = new OverviewFactory();
       final var overview = ovfac.newInstance(reader, indexPath);
       final var rec = REC(
               "commit",overview.getCommitDescription().get(),
               "fmt",overview.getIndexFormat().get(),
               "docs",overview.getNumDocuments(),
               "flds",overview.getNumFields(),
               "terms",overview.getNumTerms()
        );
       rec.foreach((key,value)->{
           System.out.println(key+"--->"+value);
       });
   }
   
   @Test
   public void documents() {
       System.out.println("\n-------documents-------");
       final var fac = new DocumentsFactory();
       final var docs = fac.newInstance(reader);
       final var rec = REC(
               "fldnames",docs.getFieldNames(),
               "term_pos",docs.getTermPositions(),
               "terms:",docs.getFieldNames().stream().map(field->{
                   List<String> ll = new LinkedList<>();
                   Stream.iterate(docs.firstTerm(field),i->docs.nextTerm())
                   .takeWhile(p->!p.isEmpty())
                   .limit(100)
                   .map(e->e.get())
                   .forEach(e->{
                       ll.add("\""+e.text()+"\"");
                   });        
                    return "\n"+field+"-->"+ll;
               }).collect(Collectors.toList())
        );
       
       rec.foreach((key,value)->{
           System.out.println(key+"\t:\t"+value);
       });
       
   }
  
   
   /**
    * 构造函数
    */
   public JunitLuke() {
       try {
           indexPath = "D:\\sliced\\tmp\\crawlerindexes";
           reader = DirectoryReader.open(FSDirectory.open(Path.of(indexPath)));
       }catch(Exception e) {
           e.printStackTrace();
       }
   }
   
   private String indexPath;
   private IndexReader reader;

}
